package cn.itsource.pethome.pet.controller;

import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.query.PetQuery;
import cn.itsource.pethome.pet.service.IPetService;
import cn.itsource.pethome.utils.AjaxResult;
import cn.itsource.pethome.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Pet pet){
        if (pet.getId() == null){
            petService.save(pet);
        }else {
            petService.update(pet);
        }
        return AjaxResult.me();
    }

    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        petService.delete(id);
        return AjaxResult.me();
    }

    @GetMapping(value = "/{id}")
    public Pet selectById(@PathVariable("id") Long id){
        return petService.loadById(id);
    }

    @GetMapping
    public List<Pet> seleceAll(){
        return petService.loadAll();
    }

    @PostMapping
    public PageResult<Pet> seleceByPage(@RequestBody PetQuery query){
        System.out.println(query);
        return petService.selectByPage(query);
    }

    @PatchMapping
    public AjaxResult deletes(@RequestBody Long[] ids){
        petService.deletes(ids);
        return AjaxResult.me();
    }

    @PatchMapping("/batchOnSale")
    public AjaxResult batchOnSale(@RequestBody Long[] ids){
        return petService.batchOnSale(ids);
    }

    @PatchMapping("/batchOffSale")
    public AjaxResult batchOffSale(@RequestBody Long[] ids){
        return petService.batchOffSale(ids);
    }

}
